package com.htl.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author hehehe
 * @ClassName JDBCController
 * @Description TODO
 * @date 2022/8/9 0009 15:05
 * @Version 1.0
 */

@RestController
public class JDBCController {
    @Autowired
    JdbcTemplate jdbcTemplate;

    //查询数据库所有信息

    @GetMapping("/userList")
    public List<Map<String, Object>> userList() {
        String sql = "select * from user";
        List<Map<String, Object>> mapList = jdbcTemplate.queryForList(sql);
        return mapList;
    }

    @GetMapping("/userAdd")
    public String userAdd() {
        String sql = "insert into user(id,username,password) values(10,'zhangsan',456)";
        jdbcTemplate.update(sql);
        return "userAdd OK";
    }

    @GetMapping("/userUpd/{id}")
    public String userUpd(@PathVariable Integer id) {
        String sql = "update user set username=?,password=? where id=" + id;
        Object[] objects = new Object[2];
        objects[0] = "李四";
        objects[1] = "789";
        jdbcTemplate.update(sql, objects);
        return "userUpd OK";
    }

    @GetMapping("/userDel/{id}")
    public String userDel(@PathVariable Integer id) {
        String sql = "delete from user where id=" + id;
        jdbcTemplate.update(sql);
        return "userDel OK";
    }
}
